//
//  MorePingLunController.m
//  ShopDemo
//
//  Created by 飞扬的青春 on 2018/8/3.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "MorePingLunController.h"
#import "PingLunwCell.h"
#import "PingLunDetailCon.h"
@interface MorePingLunController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *listTableview;

@property(nonatomic,strong)NSMutableArray *contentArray;

@property(nonatomic,assign)NSInteger index;
@property(nonatomic,assign)NSInteger page;

@end

@implementation MorePingLunController
-(NSMutableArray *)contentArray{
    if (!_contentArray) {
        _contentArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _contentArray;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.index = 1;
    self.page = 0;
    self.contentArray = [NSMutableArray arrayWithCapacity:10];
    [self contentDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:self.urlstr]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableviewMethod];
    self.listTableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (self.page == 10 ) {
            self.index ++;
            if ([self.navigationItem.title isEqualToString:@"社区评论"]) {
                 NSString *strurl = [NSString stringWithFormat:@"/dashop/newsrate/app/list.do?NEWS_ID=%@&CurrentPage=%ld&NEWSRATE_PARENTID=0",self.condic[@"NEWS_ID"],self.index];
                 [self contentDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];
            }else if ([self.navigationItem.title isEqualToString:@"商品评论"]){
                NSString *strurl = [NSString stringWithFormat:@"/dashop/goodsrate/app/list.do?GOODS_ID=%@&CurrentPage=%ld",self.condic[@"GOODS_ID"],self.index];
                [self contentDataMethodWithUrl:[MethodClass returnUrlSetStrWSDLURL:strurl]];
            }
        }else{
            [self.listTableview.mj_footer endRefreshingWithNoMoreData];
        }
    }];
}

//评论列表
-(void)contentDataMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSArray *arr = response;
        self.page = arr.count;
        for (NSDictionary *dic in arr) {
            [self.contentArray addObject:dic];
        }
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            [self.listTableview reloadData];
        }];
    } failBlock:^(NSError *error) {
        
    }];
    
}

-(void)createTableviewMethod{
    
    self.listTableview.dataSource = self;
    self.listTableview.delegate = self;
    [self.listTableview registerClass:[PingLunwCell class] forCellReuseIdentifier:@"onecell"];
    //分隔线
    self.listTableview.separatorStyle = UITableViewCellAccessoryNone;
    //去掉尾部多余的空白
    [self.listTableview setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
  
}

#pragma mark --UITableViewDelegate,UITableViewDataSource--
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return 2;
//}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.contentArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PingLunwCell *cell = [tableView dequeueReusableCellWithIdentifier:@"onecell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *dic = self.contentArray[indexPath.row];
    if ([self.navigationItem.title isEqualToString:@"社区评论"]) {
        cell.titlab.text = [NSString stringWithFormat:@"%@",dic[@"NEWSRATE_USERIDNAME"]];
        cell.datelab.text = [NSString stringWithFormat:@"%@",dic[@"NEWSRATE_ADDTIME"]];
        cell.contentlab.text = [NSString stringWithFormat:@"%@",dic[@"NEWSRATE_CONTENT"]];
    }else{
        cell.titlab.text = [NSString stringWithFormat:@"%@",dic[@"NAME"]];
        cell.datelab.text = [NSString stringWithFormat:@"%@",dic[@"RATE_ADDTIME"]];
        cell.contentlab.text = [NSString stringWithFormat:@"%@",dic[@"RATE_CONTENT"]];
    }
    
    cell.titimagevi.layer.masksToBounds = YES;
    cell.titimagevi.layer.cornerRadius = cell.titimagevi.width/2;
    NSString *strurl = [NSString stringWithFormat:@"/dashop/%@",dic[@"SFID"]];
    NSString *urlstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
    [cell.titimagevi sd_setImageWithURL:[NSURL URLWithString:urlstr]];
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100*kJLXWidthScale;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.navigationItem.title isEqualToString:@"社区评论"]) {
        NSDictionary *dic = self.contentArray[indexPath.row];
        PingLunDetailCon *deailvc = [[PingLunDetailCon alloc]init];
        deailvc.maindic = dic;
        [self.navigationController pushViewController:deailvc animated:YES];
    }
}


@end
